/* 引用jsencrypt.js */
import { JSEncrypt } from '../lib/jsencrypt/jsencrypt'

/* pkcs1格式:-----BEGIN RSA PRIVATE KEY-----xxxx-----END RSA PRIVATE KEY------ */
/* pkcs8格式: -----BEGIN PRIVATE KEY-----xxxx-----END PRIVATE KEY------*/  //用于响应Java的前端
var _PrivateKey = '-----BEGIN PRIVATE KEY-----MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK6QLVh1ZhDokKQBw6r7gjl+dGjb+Oq0f5TdMWMe1uV9JX1mQcKBXEu27EWyXaMb1V7h310EZsEOk+09kusjq2dxNJAGSha9JLre2HLLk7SdM42hMpzYC8obiWNFb44bxs2/eJvGzmX2IZhuavraf0CiRpRULdjpN+u1qaExHYC9AgMBAAECgYAQAWY2+yStnjVM5GCFd+Qb021nA+xR0NT12EYj+Xrotc3PLqYO6H+G7bHCPjV5tB6fQYSSacnmH6kOkpu69Z1Miv2KX2aUhja8I99YuCNBDo8JppW6tmQb2Sfo2WAVcsJfQvBUEKO1Kr4gPQf7KeVS7n6NXeMl+0TVnyhADn80PQJBANfLg64muvezH+VWKqVrSw8sNBB9qR5v+8C/DucNEtC/ekCmYnh8dgspq9YxeRUMI7EP+04lzPJQHPLB9so3E+cCQQDPFhXN+rf48IEA8ZPgSlvCbROK2qSQYHNCOAgOFNME07DTJkdoqIOlIUrTQ0Ew1Jh0lF9SGRfrBvR3E9Xo/3G7AkBl6pA+Z7xV0Bt4TMHHbmz8tXvBbhe5cFoOJi37oYkJXeV3Mx2+mcmGbB/kAWmWLy2MKmgd+Gxa5MtHO6Q6zNhxAkBtn85ZJOEg+ggAfOIAOLzMdruCpO26tCQvqMOy2JlAk+OmUzw4DgTqdG5M2ex3sgMmq6iuKDR+KUdehfSWDCujAkBKbD9Umg6DDCzWQsrAtm9wEt9uj1F9lTNlira3cye0KnQkbSHaQP0Bw9mPQdiDQxhZ62UNGNGB471Lyz36wytd-----END PRIVATE KEY-----'
var _PubKey = '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCodhipA7Ptytbo2GuJ7DK9FU31H3X2NYKCcvkMVydMTVVdu5TaWaZ6RPYUEieevcfqm8zcAiyZ+Xu35Gm8OvY9+Y5mtWp26XxdF9yTNBGIHx721oOC0K/bRiy9ypVhLyj7NlNRSEdDjmRIBoxdyDXg58XaSysSqRQt/0k0M65K2QIDAQAB-----END PUBLIC KEY-----'


// 加密
const rsaEncrypt = str =>{
  let encrypt = new JSEncrypt();
  encrypt.setPublicKey(_PubKey);
  str = encodeURIComponent(str);
  let encrypted = encrypt.encryptLong(str);
  // console.log("▶ 加密结果",encrypted)
  return encrypted;
}

// 解密
const rsaDecrypt = str =>{
  let encrypt = new JSEncrypt();
  encrypt.setPrivateKey(_PrivateKey);
  let decryptedStr = encrypt.decryptLong(str);
  decryptedStr = decodeURIComponent(decryptedStr)
  // console.log("▶ 解密结果",decryptedStr)
  return decryptedStr;
}

module.exports = {  
  rsaEncrypt: rsaEncrypt,
  rsaDecrypt: rsaDecrypt
}  